"""
Case Type   : MOT
Case Name   : 结合MOT表，结合系统表或系统视图查询内存表信息
Create At   : 2021/1/6
Owner       : opentestcase001
Description :
    1、检查参数，修改配置，并重启数据库
       gs_guc set -D cluster/dn1 -c "enable_incremental_checkpoint=off"
    2、创建MOT内存表，与系统表pg_class结合测试
    3、创建和内存表关联视图，与系统表pg_class结合测试
    4、清理环境
Expect      :
    1、修改成功，重启数据库成功；
    2、创建内存表成功，系统表中可以查到相关信息；
    3、创建关联视图成功；
    4、清理环境成功；
History     :
   modified by opentestcase010 at 2023/2/6，优化MOT用例
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class MotDdlTest(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.config = 'enable_incremental_checkpoint'
        self.t_name = 't_mot_0102'
        self.v_name = 'v_mot_0102'
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('-----step1:修改配置，并重启数据库-----')
        show_msg = self.common.show_param(self.config)
        if 'off' not in show_msg:
            msg1 = self.primary_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.config}=off')
            self.logger.info(msg1)
            msg2 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg2)
            msg3 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg3)
            show_msg = self.common.show_param(self.config)
            self.assertTrue('off' in show_msg)

    def test_mot_ddl(self):
        text = '-----step2:创建MOT内存表，与系统表pg_class结合测试;' \
               'expect:成功-----'
        self.logger.info(text)
        sql_cmd1 = f'''
            drop foreign table if exists {self.t_name};
            create foreign table {self.t_name}(id int,name varchar(10));
            insert into {self.t_name} values(generate_series(1,200),'a');
            select relnatts from pg_class where relname='{self.t_name}';
            select relkind from pg_class where relname='{self.t_name}';
            '''
        msg4 = self.primary_sh.execut_db_sql(sql_cmd1)
        self.logger.info(msg4)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg4)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg4)
        self.assertEqual('2', msg4.splitlines()[-7].strip())
        self.assertEqual('f', msg4.splitlines()[-2].strip())

        text = '-----step3:创建和内存表关联视图，与系统表pg_class结合测试;' \
               'expect:成功-----'
        self.logger.info(text)
        sql_cmd2 = f'''
            create view {self.v_name} as
            select * from {self.t_name} where id < 100;
            select relnatts from pg_class where relname='{self.v_name}';
            select relkind from pg_class where relname='{self.v_name}';
            '''
        msg5 = self.primary_sh.execut_db_sql(sql_cmd2)
        self.logger.info(msg5)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG, msg5)
        self.assertEqual('2', msg5.splitlines()[-7].strip())
        self.assertEqual('v', msg5.splitlines()[-2].strip())

        text = '-----step4:清理环境;expect:成功-----'
        self.logger.info(text)
        sql_cmd3 = f'''
                    drop foreign table {self.t_name} cascade;
                    '''
        msg6 = self.primary_sh.execut_db_sql(sql_cmd3)
        self.logger.info(msg6)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg6)

    def tearDown(self):
        self.logger.info('-----this is tearDown-----')
        self.logger.info('-----恢复配置，enable_incremental_checkpoint=on-----')
        show_msg = self.common.show_param(self.config)
        if 'on' not in show_msg:
            msg7 = self.primary_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.config}=on')
            self.logger.info(msg7)
            msg8 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg8)
            msg9 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg9)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('on' in show_msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
